#include <cmath>
#include <vector>
#include <iostream>
#include <vector>
#ifdef WIN32
#include <GL/glew.h>
#include <GL/glut.h>
#else
#include <GL/glew.h>
#include <GL/glut.h>
#endif

#include "utility.h"
#include "mesh.h"

void index2barycentric (unsigned res, unsigned idx, unsigned & x, unsigned & y)
{
	unsigned count = 0;
	for ( unsigned int i = 0; i <= res; ++i)
	{
		for (unsigned int j = 0 ;j <= res - i ; ++j)
		{
			if (count == idx)
			{
				x = i; y = j ;
				return;
			}
			count++;
		}
	}
}

void barycentric2index (unsigned res,  unsigned i, unsigned j, unsigned & index)
{
	index = (res + 1 + res + 2 - i ) * i / 2 + j; 
}
float randreal()
{
	return (float(rand()% 32768)/float(32767));
}
RGB rand_color(RGB & color) 
{
	color = RGB (rand () % 256, rand () % 256, rand () % 256) ;
	return color;
}
void drawsphere(float x, float y, float z, float radius) 
{
	glTranslatef(x ,y,z);
	glutSolidSphere(radius,10,10);
	glTranslatef(- x , -y, -z);
}
void getGLVersion(int * major, int * minor)
{
	const char * verstr = (const char *) glGetString (GL_VERSION) ;
	if ((verstr == NULL) || (sscanf(verstr, "%d.%d", major, minor) != 2))
	{
		* major = * minor = 0;
		fprintf(stderr, "Invalid GL_VERSION format!!! \n");
	}
}
void getGLSLVerstion(int * major, int * minor)
{
	int gl_major, gl_minor;
	getGLVersion(& gl_major, &gl_minor);
	if (gl_major >= 2)
	{
		const char * verstr = (const char *) glGetString (GL_SHADING_LANGUAGE_VERSION);
		if ((verstr == NULL) || (sscanf(verstr, "%d.%d", major, minor) != 2))
		{	
			* major = * minor = 0;
			fprintf(stderr, "Invalid GL_VERSION format!!! \n");
		}
	}
}
float compute_texture(Point p1, Point p2, Point p3 ) 
{
	return 0.0;
}